package com.qxy.hgz.user.vm

import androidx.lifecycle.ViewModel
import java.util.*

/**
 * @Description:    BaseViewModel
 * @Author:         agan
 * @CreateDate:     11/4/21 2:09 PM
 */


abstract class BaseViewModel : ViewModel() {
    private var requests: LinkedList<RequestAdapter<*>>? = null

    init {
        requests = LinkedList()
    }

    /**
     * 添加请求
     */
    protected fun managerRequest(request: RequestAdapter<*>?) {
        if (request != null) {
            request.request()
            requests?.add(request)
        }
    }

    /**
     * 取消所有网络请求
     */
    fun cancelAllRequest() {
        if (requests != null && requests!!.size > 0) {
            for (request in requests!!) {
                request.cancel()
            }
            requests!!.clear()
        }
    }

    override fun onCleared() {
        super.onCleared()
        cancelAllRequest()
    }

}